package vg.memo.mixi.util;

import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.SAXException;

import vg.memo.mixi.util.exception.MixiUtilException;
import vg.memo.mixi.util.exception.NotLoginedException;
import vg.memo.util.WebConversationUtils;

import com.meterware.httpunit.HttpUnitOptions;
import com.meterware.httpunit.PostMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

public class MixiUtils {

	private static final String HOME_URL = "http://mixi.jp/home.pl";
	private static final String LOGIN_URL = "http://mixi.jp/login.pl";
	private static final String LOGOUT_URL = "http://mixi.jp/logout.pl";
	
	protected Log log = LogFactory.getLog(MixiUtils.class);
	
	private String loginid;
	private String password;
	
	private WebConversation client;
	
	public MixiUtils(String loginid, String password) {
		this.loginid = loginid;
		this.password = password;
		
		initClient();
	}
	
	private void initClient() {
		client = new WebConversation();
		client.getClientProperties().setAutoRedirect(true);
		client.getClientProperties().setAutoRefresh(true);
		client.getClientProperties().setAcceptCookies(true);
		HttpUnitOptions.setExceptionsThrownOnScriptError(false);
	}
	
	public void login() {
		log.info("Start Login");

		WebRequest req = new PostMethodWebRequest(LOGIN_URL);
		req.setParameter("email", loginid);
		req.setParameter("password", password);
		req.setParameter("next_url", "/home.pl");
		
		WebResponse res;
		try {
			res = client.getResponse(req);
		} catch (Exception e) {
			throw new MixiUtilException(e);
		}
		
		if (isLoginPage(res))
			throw new NotLoginedException();
		
		log.info("Finish Login");
	}
	
	public void logout() throws Exception {
		log.info("Start Logout");
		client.getResponse(LOGOUT_URL);
		client.clearContents();
		log.info("Finish Logout");
	}
	
	public int numOfWaitAcceptFriends() throws Exception {
		WebResponse res = client.getResponse(HOME_URL);
		if (isLoginPage(res)) 
			throw new NotLoginedException();
		return 0;
	}
	
	public boolean isLoginPage(WebResponse res) {
		WebForm[] forms = getWebForms(res);
		for (WebForm form : forms) {
			return (form.getAction() == "./login.pl");
		}
		return false;
	}
	
	private WebForm[] getWebForms(WebResponse res) throws MixiUtilException {
		try {
			 return res.getForms();
		} catch (SAXException e) {
			e.printStackTrace();
			throw new MixiUtilException(e);
		}
	}
	
	public WebLink[] getNewFriendDialies() {
		WebResponse res = WebConversationUtils.getResponse(client, "http://mixi.jp/new_friend_diary.pl");
		ArrayList<WebLink> result = new ArrayList<WebLink>();
		for (WebLink link : WebConversationUtils.getLinks(res)) {
			if (link.getURLString().startsWith("view_diary.pl?id="))
				result.add(link);
		}
		return result.toArray(new WebLink[0]);
	}
	
	public boolean isLogined() {
		WebResponse res = WebConversationUtils.getResponse(client, HOME_URL);
		if ( isLoginPage(res) )
			return false;
		else
			return true;
	}

}
